#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 200

typedef struct chunk
{
    char*ch;
    struct chunk*next;
}chunk;
typedef struct
{
    chunk*head,*tail;
    int curlen;
}string;
typedef int status;

string*Readstring();

void Insert(string*s1,int pos,char*s2);

void Clear(string*s);

void Print(string*s);

int main()
{
    freopen("6-2.in","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string*s1=Readstring();
        int n;
        char temp[81];
        scanf("%d %s",&n,temp);
        Insert(s1,n,temp);
        Print(s1);
        Clear(s1);
    }
    return 0;
}
/* 请在这里填写答案 */
#define min(x,y) (((x)>(y))?(y):(x))

void AddChar(string*s,char ch)
{
    if(s->curlen%N==0)
    {
        chunk*c=(chunk*)malloc(sizeof(chunk));
        c->ch=(char*)malloc((sizeof(char))*N);
        c->next=NULL;
        if(s->tail==NULL)
            s->head=c;
        else
            s->tail->next=c;
        s->tail=c;
    }
    s->tail->ch[s->curlen%N]=ch;
    ++s->curlen;
}
void Add(string*s1,char*s2,int slen)
{
    for(int i=0;i<slen;++i)
        AddChar(s1,s2[i]);
}

string*Newstring()
{
    string*s=(string*)malloc(sizeof(string));
    s->curlen=0;
    s->head=NULL;
    s->tail=NULL;
    return s;
}

string*Readstring()
{
    string*s=Newstring();
    for(;;)
    {
        char ch=getchar();
        
        
        
        if(ch==EOF)
            break;
        if(ch=='\n'&&s->curlen!=0)
            break;
        if(ch!='\n')
            AddChar(s,ch);
    }
    return s;
}

void ClearWithoutS(string*s)
{
    for(chunk*p=s->head;p;)
    {
        chunk*tmp=p->next;
        free(p->ch);
        free(p);
        p=tmp;
    }
}

void Insert(string*s1,int pos,char*s2)
{
    int slen=strlen(s2);
    if(pos==s1->curlen)
        Add(s1,s2,slen);
    else if(slen)
    {
        string*ans=Newstring();
        int len=0;
        for(chunk*p=s1->head;p&&len+N<=pos;ans->tail=p,s1->head=p=p->next,len+=N)
            if(ans->head==NULL)
                ans->head=p;
        ans->curlen=len;
        Add(ans,s1->head->ch,pos-len);
        Add(ans,s2,slen);
        Add(ans,s1->head->ch+pos-len,(pos==len)?0:(N-(pos-len)));
        if(pos!=len)
            s1->head=s1->head->next;
        if(ans->curlen%N==0)
        {
            ans->tail->next=s1->head;
            s1->head=NULL;
        }
        else
        {
            for(chunk*p=s1->head;p;p=p->next)
                for(int i=0;i<N&&len<s1->curlen;++i,++len)
                    AddChar(ans,p->ch[i]);
        }
        
        
        ClearWithoutS(s1);
        s1->tail=ans->tail;
        s1->head=ans->head;
        s1->curlen+=slen;
        ans->tail=NULL;
        ans->head=NULL;
        Clear(ans);
    }
}

void Clear(string*s)
{
    ClearWithoutS(s);
    free(s);
}

void Print(string*s)
{
//    printf("%d\t",s->curlen);
    int len=0;
    for(chunk*p=s->head;p;p=p->next)
        for(int i=0;i<N&&len<s->curlen;++i,++len)
            putchar(p->ch[i]);
    putchar('\n');
}

